package com.gin.no_gay_alliance.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.MenuInflater;

import androidx.appcompat.widget.Toolbar;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.adpter.emote.EmoteItemAdapter;
import com.gin.no_gay_alliance.databinding.ActivityEmotePreviewBinding;
import com.gin.no_gay_alliance.databinding.MergeToolbarBinding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.itemdecoration.SimpleItemDecoration;
import com.gin.no_gay_alliance.utils.ViewUtils;
import com.gin.no_gay_alliance.viewmodel.emote.EmoteGroupModel;

import java.util.Locale;

/**
 * 预览表情包
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/30 14:33
 **/
public class EmotePreviewActivity extends BaseBindingActivity<ActivityEmotePreviewBinding> {
    public static final String DATA_KEY = EmoteGroupModel.class.getName();
    EmoteGroupModel emoteGroupModel;

    @Override
    protected void handleIntent(Intent intent) {
        emoteGroupModel = intent.getParcelableExtra(DATA_KEY);
        if (emoteGroupModel == null) {
            throw new RequireParamException();
        }
    }

    @Override
    protected void onCreate() {
        b.recyclerView.setAdapter(new EmoteItemAdapter(this, emoteGroupModel.emotes, null));
        b.recyclerView.addItemDecoration(new SimpleItemDecoration());

        b.tvAuthor.setText(String.format("作者: %s", emoteGroupModel.author));
        b.tvVersion.setText(String.format("v%s", emoteGroupModel.version));
        b.tvSource.setText(Html.fromHtml(String.format(Locale.CHINA, "<a href='%s'>来源</>", emoteGroupModel.source), Html.FROM_HTML_MODE_COMPACT));
        b.tvSource.setMovementMethod(LinkMovementMethod.getInstance());

        ViewUtils.setGone(ObjectUtils.isEmpty(emoteGroupModel.author), b.tvAuthor);
        ViewUtils.setGone(ObjectUtils.isEmpty(emoteGroupModel.source), b.tvSource);
        ViewUtils.setGone(ObjectUtils.isEmpty(emoteGroupModel.version), b.tvVersion);
    }

    @Override
    protected void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {

    }

    @Override
    protected Toolbar initToolbar() {
        return MergeToolbarBinding.bind(b.getRoot()).toolbar;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setToolbarTitle(emoteGroupModel.name);
    }


    public static void startActivity(Context context, EmoteGroupModel emoteGroupModel) {
        Intent intent = new Intent(context, EmotePreviewActivity.class);
        intent.putExtra(DATA_KEY, emoteGroupModel);
        context.startActivity(intent);
    }
}
